package acceptTests;

import java.util.Iterator;
import java.util.List;

import exceptions.comment.NonExistentCommentException;

import Search.SearchFacade;
import sessionManager.SessionManager;
import user.UserIF;
import user.User;
import user.blog.BlogIF;
import user.post.Post;
import user.post.comment.Comment;
import user.post.comment.CommentManager;

public class US14Facade {

	private SessionManager session;
	private SearchFacade search;
	private CommentManager CM;

	public US14Facade() {
		session = SessionManager.getInstance();
		search = new SearchFacade();
		CM = new CommentManager();
	}

	public void cleanPersistence() {
		session.cleanPersistence();

	}

	public void createProfile(String login, String senha, String nome_exibicao,
			String email, String sexo, String dataNasc, String endereco,
			String interesses, String quem_sou_eu, String filmes,
			String musicas, String livros) throws Exception {
		session.createUser(login, senha, nome_exibicao, email, sexo, dataNasc,
				endereco, interesses, quem_sou_eu, filmes, musicas, livros);
	}

	public String logon(String login, String password) throws Exception {
		return session.loginUser(login, password);
	}

	public String createBlog(String idSession, String title, String description)
			throws Exception {
		return session.getUser(idSession).createBlog(idSession, title,
				description);
	}

	public String createSubBlog(String sessionId, String blogPai, String title,
			String descricao) throws Exception {
		UserIF user = session.getUser(sessionId);

		return ((User) user).getBlogRecursively(blogPai).addSubBlog(title,
				descricao);
	}

	public Integer getNumberOfBlogsByLogin(String login) throws Exception {
		return session.getUserByLogin(login).getBlogs().size();
	}

	public Integer getNumberOfBlogsBySessionId(String login) throws Exception {
		return session.getUser(login).getBlogs().size();
	}

	public int getNumberOfSubBlogs(String blogId) throws Exception {

		BlogIF blog = search.findBlogByID(blogId);

		if (blog != null)
			return blog.getSubBlogs().size();

		return 0;
	}

	public Integer getNumberOfAllSubBlogs(String blogId) throws Exception {
		return search.getNumberAllSubBlogs(blogId);
	}

	public String getSubBlog(String blogId, int index) throws Exception {
		BlogIF blog = search.findBlogByID(blogId);

		if (blog != null) {
			if (blog.getSubBlogs().size() != 0)
				return blog.getSubBlogs().get(index).getId();
		}

		return null;
	}

	public String createPost(String sessionId, String blogId, String titulo,
			String texto) throws Exception {

		UserIF user = session.getUser(sessionId);
		String IDpostCriado = ((User) user).getBlogRecursively(blogId).addPost(
				titulo, texto);

		return IDpostCriado;
	}

	public int getNumberOfPosts(String blogId) throws Exception {

		BlogIF blog = search.findBlogByID(blogId);

		if (blog != null)
			return blog.getPostManager().getNumberOfPosts();

		return 0;

	}

	public int getNumberOfAllPosts(String blogId) throws Exception {
		List<BlogIF> subBlogs = search.getAllSubBlogs(blogId);
		BlogIF blog = search.findBlogByID(blogId);
		int numberOfPosts = 0;

		if (blog != null)
			numberOfPosts = blog.getPostManager().getNumberOfPosts();

		for (BlogIF b : subBlogs)
			numberOfPosts += b.getPostManager().getNumberOfPosts();

		return numberOfPosts;
	}

	public String addComment(String sessionId, String postId, String text)
			throws Exception {

		Iterator<UserIF> itUsers = session.getUsers().iterator();

		while (itUsers.hasNext()) {
			UserIF tmpUser = itUsers.next();
			Iterator<BlogIF> itBlog = tmpUser.getBlogs().iterator();
			while (itBlog.hasNext()) {

				BlogIF tmpBlog = itBlog.next();

				for (Post pos : tmpBlog.getPostManager().getPosts()) {

					if (postId.equals(pos.getId())) {

						return pos.addComment(session.getUser(sessionId)
								.getLogin(), text);
					}
				}

				for (BlogIF b : search.getAllSubBlogs(tmpBlog.getId()))
					for (Post pos : b.getPostManager().getPosts()) {

						if (postId.equals(pos.getId()))
							return pos.addComment(session.getUser(sessionId)
									.getLogin(), text);

					}
			}
		}

		return null;
	}

	public String addSubComment(String sessionId, String idComment, String text)
			throws Exception {

		Iterator<UserIF> itUsers = session.getUsers().iterator();

		while (itUsers.hasNext()) {
			UserIF tmpUser = itUsers.next();
			Iterator<BlogIF> itBlog = tmpUser.getBlogs().iterator();
			while (itBlog.hasNext()) {
				BlogIF tmpBlog = itBlog.next();
				Iterator<Post> itPost = tmpBlog.getPostManager().getPosts()
						.iterator();
				while (itPost.hasNext()) {
					Post post = itPost.next();
					if (post.getCommentManager().findCommentByID(idComment) != null)
						return post.getCommentManager().addSubComment(
								session.getUser(sessionId).getLogin(), text,
								idComment);

				}

				for (BlogIF b : search.getAllSubBlogs(tmpBlog.getId())) {
					Iterator<Post> itPos = b.getPostManager().getPosts()
							.iterator();
					while (itPos.hasNext()) {
						Post post = itPos.next();
						if (post.getCommentManager().findCommentByID(idComment) != null)
							return post.getCommentManager().addSubComment(
									session.getUser(sessionId).getLogin(),
									text, idComment);

					}
				}

			}
		}

		return null;
	}

	public int getNumberOfSubComments(String idComment)
			throws NonExistentCommentException {

		return CM.getNumberOfSubComments(idComment);
	}

	public int getNumberOfAllSubComments(String idComment)
			throws NonExistentCommentException {
		return CM.getNumberOfAllSubComments(idComment);
	}

	public String getSubComment(String idComment, int index)
			throws NonExistentCommentException {

		Comment coment = CM.findCommentByID(idComment);

		if (coment != null)
			return CM.findCommentByID(idComment).getComments().get(index)
					.getId();

		return null;
	}
}